package com.study.dto;

import cn.hutool.bloomfilter.BitMapBloomFilter;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import io.netty.channel.ChannelOption;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;
import reactor.netty.resources.ConnectionProvider;
import reactor.netty.tcp.TcpClient;

import java.time.Duration;
import java.time.LocalDate;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author zhuzi
 * 2024/12/21 10:1
 * 节假日|包括周末
 * https://timor.tech/api/holiday/year/2024?type=Y&week=Y
 */
public interface HolidayRootDTO {


    class HolidayTool {

        /**
         * 节假日
         **/
        public static String holidayUrl = "https://timor.tech/api/holiday/year/{}?type=Y&week=Y";
        public static BitMapBloomFilter holidayBloomFilter = new BitMapBloomFilter(10);
        public static WebClient webClient = null;

        static {
            ConnectionProvider provider = ConnectionProvider.builder("fixed").maxConnections(200) // 最大连接数
                    .maxIdleTime(Duration.ofSeconds(60)) // 最大空闲时间
                    .maxLifeTime(Duration.ofSeconds(60)) // 最大存活时间
                    .pendingAcquireTimeout(Duration.ofSeconds(60)) // 等待获取时间
                    .pendingAcquireMaxCount(1000) // 挂起的获取最大计数
                    .build();

            TcpClient tcpClient = TcpClient.create(provider)//
                    .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000) // 连接超时时间
                    .doOnConnected(connection -> {
                        connection.addHandlerLast(new ReadTimeoutHandler(5000, TimeUnit.MILLISECONDS)); // 读取超时
                        connection.addHandlerLast(new WriteTimeoutHandler(5000, TimeUnit.MILLISECONDS)); // 写入超时
                    });

            HttpClient httpClient = HttpClient.from(tcpClient).keepAlive(true) // 启用保持连接
                    .responseTimeout(Duration.ofSeconds(10)); // 响应超时时间
            webClient = WebClient.builder().clientConnector(new ReactorClientHttpConnector(httpClient)).build();
        }

    }

    public static void main(String[] args) {
        getHoliday();
        System.out.println(HolidayTool.holidayBloomFilter.contains(LocalDate.now().toString()));
    }

    static List<String> getHoliday() {
        String holdayStr = HolidayTool.webClient.get().uri(StrUtil.format(HolidayTool.holidayUrl, LocalDate.now().getYear()))
                .retrieve().bodyToMono(String.class)//
                .timeout(Duration.ofSeconds(8)).retry(3).block();

        JSONObject jsonObject = JSON.parseObject(holdayStr);
        JSONObject type = jsonObject.getJSONObject("type");

        List<String> holidayList = type.keySet().stream().map(ia -> {
            HolidayTool.holidayBloomFilter.add(ia);
            return ia;
        }).collect(Collectors.toList());
        return holidayList;
    }
}
